package com.shundr.shipper.user;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.TextView;

import com.shundr.shipper.R;
import com.shundr.shipper.base.BaseActivity;
import com.shundr.shipper.common.util.ProgressDialogHelper;
import com.shundr.shipper.common.util.ToastHelper;
import com.shundr.shipper.common.view.CListView;
import com.shundr.shipper.common.view.CListView.OnLoadMoreListener;
import com.shundr.shipper.common.view.CListView.OnRefreshListener;
import com.shundr.shipper.frame.constant.LContext;
import com.shundr.shipper.frame.http.LHttpRequest;
import com.shundr.shipper.user.adapter.RecordAdapter;
import com.shundr.shipper.user.http.UserRequest;
import com.shundr.shipper.user.model.RecordInfo;

/**
 * 
 * CreditsExchangeActivity
 * 
 * @author larry 2015-7-28 下午5:21:17 
 * @version 1.0.0
 *
 */
public class CreditsExchangeActivity extends BaseActivity{

	private static final int PAGE_SIZE = LContext.PAGE_SIZE;
	private int pageNo = 1;
	
	private TextView tvFlag;
	
	private CListView mListView;
	private RecordAdapter mAdapter;
	private List<RecordInfo> mList = new ArrayList<RecordInfo>();
	
	private Handler mHandler = new Handler(){

		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			switch (msg.what) {
			case UserRequest.GET_EXCHANGE_LIST_SUCCESSED:
				List<RecordInfo> infos = (List<RecordInfo>)msg.obj;
				if (infos != null) {
					if (infos.size() > 0) {
						if (pageNo == 1) {
							mList.clear();
							mListView.setVisibility(View.VISIBLE);
							tvFlag.setVisibility(View.GONE);
							if (infos.size() >= PAGE_SIZE) {
								mListView.setCanLoadMore(true);
								mListView.setAutoLoadMore(true);
								mListView.setCanRefresh(true);
							} else {
								mListView.setCanLoadMore(false);
								mListView.setAutoLoadMore(false);
								mListView.setCanRefresh(false);
							}
						}
						mList.addAll(infos);
						mAdapter.notifyDataSetChanged();
						if (pageNo == 1) {
							mListView.setSelection(0);
						}
						pageNo = pageNo + 1;
					} else {
						if (pageNo == 1) {
							mListView.setVisibility(View.GONE);
							tvFlag.setVisibility(View.VISIBLE);
						} else {
							ToastHelper.toastLong(mContext, "最后一页");
						}
					}
				}
				ProgressDialogHelper.dismiss();
				mListView.onRefreshComplete();
				mListView.onLoadMoreComplete();
				break;
			case LHttpRequest.REQUEST_FAILED:
				ProgressDialogHelper.dismiss();
				ToastHelper.toastShort(mContext, msg.obj.toString());
				break;

			default:
				break;
			}
		}
		
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_credits_record);
		initView();
		ProgressDialogHelper.show(mContext);
		new UserRequest(mContext, mHandler).getExchangeList(pageNo, PAGE_SIZE);
	}

	private void initView() {

		tvFlag = (TextView) findViewById(R.id.tv_flag);
		tvFlag.setText("您还没有兑换记录");
		
		mListView = (CListView) findViewById(R.id.lv_list);
		mAdapter = new RecordAdapter(mList, mContext);
		mListView.setAdapter(mAdapter);
		
		mListView.setOnRefreshListener(new OnRefreshListener() {
			
			@Override
			public void onRefresh() {
				pageNo = 1;
				new UserRequest(mContext, mHandler).getExchangeList(pageNo, PAGE_SIZE);
			}
		});
		mListView.setOnLoadListener(new OnLoadMoreListener() {
			@Override
			public void onLoadMore() {
				new UserRequest(mContext, mHandler).getExchangeList(pageNo, PAGE_SIZE);
			}
		});	
		
	}
	
	@Override
	protected void onDestroy() {
		try {
			mList.clear();
			mList = null;
		} catch (Exception e) {
			e.printStackTrace();
		}
		super.onDestroy();
	}

}
